package com.cumt.threeway.view.dialog;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.cumt.threeway.R;
import com.cumt.threeway.activity.LoginActivity;
import com.cumt.threeway.activity.SubmitActivity;
import com.cumt.threeway.activity.WriteStrategyActivity;
import com.cumt.threeway.constant.ThreeWayConstant;

public class NeedLoginDialog extends DialogFragment implements View.OnClickListener{

	private TextView mTvContent;
	private Button mBtnLoginConfirm;
	private Button mBtnLoginCancel;
	private Context mContext;
	private String mKey;

	public static NeedLoginDialog newInstance(String key) {
		NeedLoginDialog needLoginDialog = new NeedLoginDialog();
		Bundle bundle = new Bundle();
		bundle.putString("key", key);
		needLoginDialog.setArguments(bundle);
		return needLoginDialog;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = activity;
		mKey = getArguments().getString("key");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		View view = inflater.inflate(R.layout.need_login_dialog, null, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		mTvContent = (TextView) view.findViewById(R.id.tv_site);
		mBtnLoginConfirm = (Button) view.findViewById(R.id.btn_confirm);
		mBtnLoginCancel = (Button) view.findViewById(R.id.btn_cancel);
		if("comment".equals(mKey)) {
			mTvContent.setText(getResources().getString(R.string.login_to_comment));
		} else if("checkSubmitInfo".equals(mKey)) {
			mTvContent.setText(getResources().getString(R.string.login_to_checkSubmitInfo));
		} else if("submitFavourite".equals(mKey)) {
			mTvContent.setText(getResources().getString(R.string.login_to_checkFavourite));
		} else if("writeStrategy".equals(mKey)) {
			mTvContent.setText(getResources().getString(R.string.login_to_writeStrategy));
		} else if("submitYuDing".equals(mKey)) {
			mTvContent.setText(getResources().getString(R.string.login_to_submit));
		}
		mBtnLoginConfirm.setText(getResources().getString(R.string.go_login_message));

		mBtnLoginConfirm.setOnClickListener(this);
		mBtnLoginCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v == null) {
			return;
		}
		switch (v.getId()) {
			case R.id.btn_confirm:
				dismiss();
				if(mContext instanceof SubmitActivity) {
					if("submitFavourite".equals(mKey)) {
						((SubmitActivity)mContext).goLoginActivityForResult("submitFavourite");
					} else {
						((SubmitActivity)mContext).goLoginActivityForResult("submitYuDing");
					}
				} else if(mContext instanceof WriteStrategyActivity) {
					((WriteStrategyActivity)mContext).goLoginActivityForResult();
				} else {
					goLoginActivity();
				}
				break;
			case R.id.btn_cancel:
				dismiss();
				break;
		}
	}

	private void goLoginActivity() {
		Intent intent = new Intent(mContext, LoginActivity.class);
		intent.putExtra("key", ThreeWayConstant.INTENT_KEY_TO_LOGIN_2);
		intent.putExtra("needLogin", mKey);
		startActivity(intent);
	}
}
